package com.xinchao.ck_mips.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.orhanobut.logger.Logger;
import com.xinchao.ck_mips.constant.DeviceConstant;
import com.xinchao.devicemanage.util.DeviceDBUtil;
import com.xinchao.devicemanage.util.DeviceTool;
import com.xinchao.devicemanage.util.FileTools;

import java.io.IOException;

/*
 *Created by xiaoqiang on 2018/8/21.
 */
public class DeviceInfoUtil {

    public DeviceInfo deviceInfo;

    private static DeviceInfoUtil instance = new DeviceInfoUtil();

    public static DeviceInfoUtil getInstance() {
        synchronized (com.xinchao.devicemanage.util.DeviceInfoUtil.class) {
            if (instance == null) {
                instance = new DeviceInfoUtil();
            }
            return instance;
        }
    }

    public DeviceInfoUtil() {
        //Logger.t(DeviceConstant.qiangTag).d("DeviceInfoUtil creat");
        init();
    }

    public void init() {
        //文件读取
        String code = FileTools.getContent(DeviceConstant.APP_MACHINE_CODE_FILE_PATH);
        if (!TextUtils.isEmpty(code)) {
            try {
                deviceInfo = new Gson().fromJson(code, DeviceInfo.class);
                if (deviceInfo != null) {
                    //Logger.t(DeviceConstant.qiangTag).d("文件读取到配置:" + deviceInfo.toString());
                    return;
                }
            } catch (JsonSyntaxException e) {
            } catch (Exception e) {
            }
        }

        String deviceId = getDeviceId();
        String deviceCode = getDeviceCode();
        int[] on_off = getDeviceOnOff();
        int deviceVolume = getVolume();

        if (deviceInfo == null)
            deviceInfo = new DeviceInfo();

        deviceInfo.setDeviceId(deviceId);
        deviceInfo.setDeviceCode(deviceCode);
        deviceInfo.setOn_h(on_off[0]);
        deviceInfo.setOn_m(on_off[1]);
        deviceInfo.setOff_h(on_off[2]);
        deviceInfo.setOff_m(on_off[3]);
        deviceInfo.setVolume(deviceVolume);
        saveDeviceInfo(deviceInfo);
        return;
    }

    /**
     * 文件存储配置信息
     *
     * @param deviceInfo
     * @return
     */
    public DeviceInfo saveDeviceInfo(DeviceInfo deviceInfo) {
        if (deviceInfo != null) {
            FileTools.createNewFile(DeviceConstant.APP_MACHINE_CODE_FILE_PATH, new Gson().toJson(deviceInfo));
            return deviceInfo;
        }
        return null;
    }

    /**
     * 获取机器码
     *
     * @return
     */
    public String getDeviceId() {
        if (!TextUtils.isEmpty(DeviceConstant.deviceId)) {
            return DeviceConstant.deviceId;
        }
        DeviceConstant.deviceId = DeviceDBUtil.getDBConfigValue(DeviceConstant.COLUMN_MACHINE_ID);
        if (!TextUtils.isEmpty(DeviceConstant.deviceId)) {
        } else {
            DeviceConstant.deviceId = DeviceConstant.DEFAULT_MACHINE_ID;
            if (deviceInfo != null)
                DeviceConstant.deviceId = deviceInfo.getDeviceId();
            DeviceDBUtil.saveDBConfig(DeviceConstant.COLUMN_MACHINE_ID, DeviceConstant.deviceId);
        }
        Logger.t(DeviceConstant.qiangTag).d("deviceId:" + DeviceConstant.deviceId);
        return DeviceConstant.deviceId.replace("\n", "");
    }

    /**
     * 存储机器码
     *
     * @param deviceId
     */
    public void saveDeviceId(String deviceId) {
        DeviceConstant.deviceId = deviceId;
        DeviceDBUtil.saveDBConfig(DeviceConstant.COLUMN_MACHINE_ID, deviceId);
        if (deviceInfo != null) {
            deviceInfo.setDeviceId(deviceId);
            saveDeviceInfo(deviceInfo);
        }
    }

    /**
     * 获取开关机时间
     *
     * @return
     */
    public int[] getDeviceOnOff() {
        String on_h = DeviceDBUtil.getDBConfigValue(DeviceConstant.COLUMN_ON_H);
        if (!TextUtils.isEmpty(on_h)) {
        } else {
            on_h = DeviceConstant.DEFAULT_ON_OFF[0] + "";
            if (deviceInfo != null)
                on_h = deviceInfo.getOn_h() + "";
            DeviceDBUtil.saveDBConfig(DeviceConstant.COLUMN_ON_H, on_h);
        }

        String on_m = DeviceDBUtil.getDBConfigValue(DeviceConstant.COLUMN_ON_M);
        if (!TextUtils.isEmpty(on_m)) {
        } else {
            on_m = DeviceConstant.DEFAULT_ON_OFF[1] + "";
            if (deviceInfo != null)
                on_m = deviceInfo.getOn_m() + "";
            DeviceDBUtil.saveDBConfig(DeviceConstant.COLUMN_ON_M, on_m);
        }

        String off_h = DeviceDBUtil.getDBConfigValue(DeviceConstant.COLUMN_OFF_H);
        if (!TextUtils.isEmpty(off_h)) {
        } else {
            off_h = DeviceConstant.DEFAULT_ON_OFF[2] + "";
            if (deviceInfo != null)
                off_h = deviceInfo.getOff_h() + "";
            DeviceDBUtil.saveDBConfig(DeviceConstant.COLUMN_OFF_H, off_h);
        }

        String off_m = DeviceDBUtil.getDBConfigValue(DeviceConstant.COLUMN_OFF_M);
        if (!TextUtils.isEmpty(off_m)) {
        } else {
            off_m = DeviceConstant.DEFAULT_ON_OFF[3] + "";
            if (deviceInfo != null)
                off_m = deviceInfo.getOff_m() + "";
            DeviceDBUtil.saveDBConfig(DeviceConstant.COLUMN_OFF_M, off_m);
        }

        return new int[]{Integer.valueOf(on_h), Integer.valueOf(on_m), Integer.valueOf(off_h), Integer.valueOf(off_m)};
    }

    /**
     * 存储开关机时间
     *
     * @param on_h
     * @param on_m
     * @param off_h
     * @param off_m
     */
    public void saveDeviceOnOff(int on_h, int on_m, int off_h, int off_m) {
        DeviceDBUtil.saveDBConfig(DeviceConstant.COLUMN_ON_H, on_h + "");
        DeviceDBUtil.saveDBConfig(DeviceConstant.COLUMN_ON_M, on_m + "");
        DeviceDBUtil.saveDBConfig(DeviceConstant.COLUMN_OFF_H, off_h + "");
        DeviceDBUtil.saveDBConfig(DeviceConstant.COLUMN_OFF_M, off_m + "");
        if (deviceInfo != null) {
            deviceInfo.setOn_h(on_h);
            deviceInfo.setOn_m(on_m);
            deviceInfo.setOff_h(off_h);
            deviceInfo.setOff_m(off_m);
            saveDeviceInfo(deviceInfo);
        }
    }

    /**
     * 获取音量
     *
     * @return
     */
    public int getVolume() {
        String deviceVolume = DeviceDBUtil.getDBConfigValue(DeviceConstant.COLUMN_MACHINE_VOLUME);
        if (!TextUtils.isEmpty(deviceVolume)) {
        } else {
            //兼容之前mips号
            deviceVolume = DeviceConstant.DEFAULT_VOLUME + "";
            if (deviceInfo != null)
                deviceVolume = deviceInfo.getVolume() + "";
            DeviceDBUtil.saveDBConfig(DeviceConstant.COLUMN_MACHINE_VOLUME, deviceVolume);
        }
        return Integer.valueOf(deviceVolume);
    }

    /**
     * 存储音量
     *
     * @param volume
     */
    public void saveVolume(int volume) {
        DeviceDBUtil.saveDBConfig(DeviceConstant.COLUMN_MACHINE_VOLUME, volume + "");
        if (deviceInfo != null) {
            deviceInfo.setVolume(volume);
            saveDeviceInfo(deviceInfo);
        }
    }

    public String getDeviceCode() {
        if (!TextUtils.isEmpty(DeviceConstant.deiceCode)) {
            return DeviceConstant.deiceCode;
        }
        try {
            if (!Build.MODEL.equals("HiDPTAndroid") && !TextUtils.isEmpty(DeviceTool.getDeviceCode())) {
                DeviceConstant.deiceCode = DeviceTool.getDeviceCode();
                return DeviceConstant.deiceCode;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        DeviceConstant.deiceCode = DeviceDBUtil.getDBConfigValue(DeviceConstant.COLUMN_MACHINE_CODE);
        if (!TextUtils.isEmpty(DeviceConstant.deiceCode)) {
        } else {
            if (deviceInfo != null) {
                DeviceConstant.deiceCode = deviceInfo.getDeviceCode() + "";
                DeviceDBUtil.saveDBConfig(DeviceConstant.COLUMN_MACHINE_CODE, DeviceConstant.deiceCode);
            } else {
                DeviceConstant.deiceCode = "";
            }
        }
        return DeviceConstant.deiceCode;
    }

    public void saveDeviceCode(String deviceCode) {
        DeviceConstant.deiceCode = deviceCode;
        DeviceDBUtil.saveDBConfig(DeviceConstant.COLUMN_MACHINE_CODE, DeviceConstant.deiceCode);
        if (deviceInfo != null) {
            deviceInfo.setDeviceCode(deviceCode);
            saveDeviceInfo(deviceInfo);
        }
    }

    public void saveToken(String token) {
        Logger.d("save token:" + token);
        DeviceConstant.token = token;
        DeviceDBUtil.saveDBConfig(DeviceConstant.COLUMN_MACHINE_TOKEN, DeviceConstant.token);
        if (deviceInfo != null) {
            deviceInfo.setToken(token);
            saveDeviceInfo(deviceInfo);
        }
    }

    /**
     * 获取token
     *
     * @return
     */
    public String getToken() {
        String token = "";
        if (TextUtils.isEmpty(DeviceConstant.token)) {
            DeviceConstant.token = deviceInfo.getToken();
        }
        token = DeviceConstant.token;
        Logger.d("get token:" + token);
        return token;
    }

    public DisplayMetrics getDisplay(Activity activity) {
        WindowManager mWindowManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        mWindowManager.getDefaultDisplay().getMetrics(metrics);
        return metrics;

    }
}
